class Event < ActiveRecord::Base
  cattr_reader :per_page
  @@per_page = 10
  
  belongs_to :submitor, :class_name => 'User', :foreign_key => 'submitor_id'
  belongs_to :school
  has_one    :pic, :class_name => 'EventPic', :dependent => :destroy
  has_many   :participation, :dependent => :destroy
  has_many   :participators, :through => :participation, :source => :user
  has_many   :comments, :class_name => "EventComment", :dependent => :destroy
  has_many   :photos
  
  validates_presence_of :title, :category, :start_at, :end_at, :location
  
  format_attribute :content
  
  def self.categories
    %w(公益旅行 物资募捐 支教 其他)
  end
  
  def editable_by?(user)
    user!=:false && (submitor_id == user.id || user.is_admin?)
  end

  def has_end?()
    end_at < Time.now
  end
  
  def self.recent_cloud
    find(:all, :order => "events.start_at desc, count desc",
                               :joins => "left join participations on events.id=participations.event_id",
                               :group => "events.id",
                               :select => "events.id, events.title, count(events.id) as count",
                               :limit => 10)
  end
end
